package server;

import java.net.DatagramPacket;
import java.util.ArrayList;
import java.util.List;

import server.messageContent.CloseReplyMsgContent;
import server.messageContent.CreateAccountReplyMsgContent;
import server.messageContent.DepositReplyMsgContent;
import server.messageContent.TransferFundReplyMsgContent;
import server.messageContent.WithdrawReplyMsgContent;
import common.CurrencyEnum;
import common.ServiceErrorException;
import common.ServiceResponseEnum;
import common.data.Message;
import common.data.MessageTypeEnum;
import common.interfaces.MessageContent;
//This object stores List of Callback clients for monitor service
//Including add and delete callback object into/out of the Callback List
//Including methods to send notification to all clients in Callback List when there is any update at server
public class MonitorService {

	List<Callback> callbackList;

	public MonitorService() {
		this.callbackList = new ArrayList<Callback>();

	}

	public void AddNewClient(Callback aClient) {

		callbackList.add(aClient);

	}

	public void DeleteClient(Callback aClient) {

		callbackList.remove(aClient);

	}

	public void PushNotification(MessageContent replyMsgContent){
		for (int i = 0; i < this.callbackList.size(); i++) {

			Message msg = new Message(MessageTypeEnum.REPLY,
					RequestHandler.increaseReplyID(), replyMsgContent);
			DatagramPacket reply = new DatagramPacket(msg.serialize(),
					msg.getLength(), callbackList.get(i).getIp(), callbackList
							.get(i).getPort());

			try {
				SocketManager.getCurrent().SendPacket(reply);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}
	
	public void CreateAccount(long accId) {
		CreateAccountReplyMsgContent createAccountReplyMsg = new CreateAccountReplyMsgContent(
				accId);		
		this.PushNotification(createAccountReplyMsg);
	}

	public void CloseAccount(ServiceResponseEnum response, long accId) {
		CloseReplyMsgContent closeAccountReplyMsg = new CloseReplyMsgContent(
				response, accId);
		this.PushNotification(closeAccountReplyMsg);

	}

//	public void Withdraw(ServiceResponseEnum response, long accId) {
//		WithdrawReplyMsgContent withdrawReplyMsg = new WithdrawReplyMsgContent(response, accId);
	public void Withdraw(ServiceResponseEnum response, long accId, float amountWithdraw, CurrencyEnum currency) throws ServiceErrorException {
		WithdrawReplyMsgContent withdrawReplyMsg = new WithdrawReplyMsgContent(response, accId, amountWithdraw, currency);		
		this.PushNotification(withdrawReplyMsg);
	}
	
//	public void Deposit(ServiceResponseEnum response, long accId) {
//		DepositReplyMsgContent depositReplyMsg = new DepositReplyMsgContent(response, accId);
	public void Deposit(ServiceResponseEnum response, long accId, float amountDeposit, CurrencyEnum currency) throws ServiceErrorException {
		DepositReplyMsgContent depositReplyMsg = new DepositReplyMsgContent(response, accId,amountDeposit, currency);
		this.PushNotification(depositReplyMsg);
	}
	
	public void BalanceEnquiry(long accId) {
		
	}
	
	public void Transfer(ServiceResponseEnum response, long accIdSender, long accIdReceiver, float amount, CurrencyEnum current) throws ServiceErrorException{
		TransferFundReplyMsgContent transferReplyMsg = new TransferFundReplyMsgContent(response, accIdSender, accIdReceiver, amount, current);
		this.PushNotification(transferReplyMsg);
	}
}
